package com.example.zengyi

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity


class HistoryActivity : AppCompatActivity() {
    private var primeListView: ListView? = null
    private var primes: MutableList<Int>? = null
    private var adapter: PrimeNumberAdapter? = null
    private var dbHelper: PrimeNumberHelper? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_history)

        primeListView = findViewById(R.id.primeListView)
        dbHelper = PrimeNumberHelper(this)

        loadPrimes()
    }

    @SuppressLint("Range")
    private fun loadPrimes() {
        val cursor = dbHelper!!.allPrimes
        primes = ArrayList()
        while (cursor.moveToNext()) {
            (primes as ArrayList<Int>).add(cursor.getInt(cursor.getColumnIndex(PrimeNumberHelper.COLUMN_PRIME)))
        }
        cursor.close()

        adapter = PrimeNumberAdapter(this, primes)
        primeListView!!.adapter = adapter
    }
}